﻿using PortfolioFinalProject.Data;
using PortfolioFinalProject.DataModels;
using PortfolioFinalProject.Web.IDataAdapters.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PortfolioFinalProject.Web.IDataAdapters.Adapters
{
    public class PortfolioAdapter : IPortfolioAdapter
    {
        public List<Skill> GetAllSkills()
        {
            List<Skill> skills;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                skills = db.Skills.OrderBy(x => x.Title).ToList();
            }
            return skills;
        }

        public Skill AddSkill(Skill skill)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                db.Skills.Add(skill);
                db.SaveChanges();
            }
            return skill;
        }

        public Skill UpdateSkill(Skill skill)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Skill oldSkill = db.Skills.Find(skill.Id);
                oldSkill.setProps(skill);
                db.SaveChanges();
            }
            return skill;
        }

        public void DeleteSkill(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Skill skill = db.Skills.Find(id);
                db.Skills.Remove(skill);
                db.SaveChanges();
            }
        }

        public List<Project> GetAllProjects()
        {
            List<Project> projects;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                projects = db.Projects.OrderBy(x => x.Title).ToList();
            }
            return projects;
        }

        public Project AddProject(Project project)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                db.Projects.Add(project);
                db.SaveChanges();
            }
            return project;
        }

        public Project UpdateProject(Project project)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Project oldProject = db.Projects.Find(project.Id);
                oldProject.setProps(project);
                db.SaveChanges();
            }
            return project;
        }

        public void DeleteProject(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Project project = db.Projects.Find(id);
                db.Projects.Remove(project);
                db.SaveChanges();
            }
        }
    }
}